package com.telemetria.service;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.primefaces.json.JSONObject;

public class WSService {
	
	public static void main(String[] args) throws  Exception {
		
		DefaultHttpClient httpclient = new DefaultHttpClient();

		HttpGet httpget = new HttpGet("http://192.168.1.177/");

        HttpResponse response = httpclient.execute(httpget);
        
        HttpEntity entity = response.getEntity();
        
        String returnString = null;
        
//        InputStream is = entity.getContent();
//        BufferedReader br = new BufferedReader(new InputStreamReader(is));
//        String str ="";
//        while ((str = br.readLine()) != null){
//            System.out.println(str);
//        }
        
        
        if (entity != null) {
            // A Simple JSON Response Read
            InputStream instream = entity.getContent();

            // Load the requested page converted to a string into a JSONObject.
            JSONObject myAwway = new JSONObject(convertStreamToString(instream));

            // Get the query value'
            String sensor1 = myAwway.getString("sensor_1");           
            String sensor2 = myAwway.getString("sensor_2");
            String sensor3 = myAwway.getString("sensor_3");
            String sensor4 = myAwway.getString("sensor_4");
            

            // Make array of the suggestions
//            JSONArray arrayJson = myAwway.getJSONArray("arrayJson");

            // Build the return string.
//            returnString = "Found: " + arrayJson.length() + " locations for " + query;
//            for (int i = 0; i < arrayJson.length(); i++) {
//                returnString += "\n\t" + arrayJson.getString(i);
//            }

            // Cose the stream.
            instream.close();
        }
    }
        

     /*   System.out.println("Login form get: " + response.getStatusLine());
        if (entity != null) {
            entity.consumeContent();
        }
        System.out.println("Initial set of cookies:");
        List<Cookie> cookies = httpclient.getCookieStore().getCookies();
        if (cookies.isEmpty()) {
            System.out.println("None");
        } else {
            for (int i = 0; i < cookies.size(); i++) {
                System.out.println("- " + cookies.get(i).toString());
            }
        }

        HttpPost httpost = new HttpPost("https://portal.sun.com/amserver/UI/Login?" +
                "org=self_registered_users&" +
                "goto=/portal/dt&" +
                "gotoOnFail=/portal/dt?error=true");

        List <NameValuePair> nvps = new ArrayList <NameValuePair>();
        nvps.add(new BasicNameValuePair("IDToken1", "username"));
        nvps.add(new BasicNameValuePair("IDToken2", "password"));

        httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

        response = httpclient.execute(httpost);
        entity = response.getEntity();

        System.out.println("Login form get: " + response.getStatusLine());
        if (entity != null) {
            entity.consumeContent();
        }

        System.out.println("Post logon cookies:");
        cookies = httpclient.getCookieStore().getCookies();
        if (cookies.isEmpty()) {
            System.out.println("None");
        } else {
            for (int i = 0; i < cookies.size(); i++) {
                System.out.println("- " + cookies.get(i).toString());
            }
        }

		
	}	*/
	
	
	private static String convertStreamToString(InputStream is) {
        /*
         * To convert the InputStream to String we use the BufferedReader.readLine()
         * method. We iterate until the BufferedReader return null which means
         * there's no more data to read. Each line will appended to a StringBuilder
         * and returned as String.
         */
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();
 
        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }
	
	

}
